package br.edu.unicentro.mymib.mbean;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import br.edu.unicentro.mymib.base.EntityManagerService;
import br.edu.unicentro.mymib.entity.Post;
import br.edu.unicentro.mymib.entity.Tag;
import br.edu.unicentro.mymib.entity.Usuario;
import br.edu.unicentro.mymib.mbean.entity.PostEntity;

public class FindPostMBean {

	private String criterioTag = "";
	private String criterioEmail = "";
	private Date dataInicial, dataFinal;
	private boolean windowVisible;
	private List<PostEntity> posts;
	private List<PostEntity> visiblePosts;
	private int pagina = 1;
	private final int NUM_POSTS = 10;
	private String selectedGroup = "byDate";

	public FindPostMBean() {
		posts = new ArrayList<PostEntity>();
		visiblePosts = new ArrayList<PostEntity>();
	}

	public void find(ActionEvent event) {
		List<Post> originalPosts = null;
		if (selectedGroup.equals("byDate")) {
			originalPosts = Post.buscarPorData(dataInicial, dataFinal);
		} else if (selectedGroup.equals("byTag")) {
			originalPosts = Tag.buscarPorNome(criterioTag).getPosts();
		} else if (selectedGroup.equals("byEmail")) {
			originalPosts = Post.buscarPorEmailDeUsuario(criterioEmail);
		}
		if (originalPosts == null) {
			originalPosts = new ArrayList<Post>();
		}
		listarPosts(originalPosts);

	}

	private void listarPosts(List<Post> originalPosts) {
		PostEntity poe;
		posts = new ArrayList<PostEntity>();
		for (Post p : originalPosts) {
			poe = new PostEntity();
			poe.setPost(p);
			posts.add(poe);
		}
		filtrar();
	}

	public void exibirWindow(ActionEvent event) {
		clear();
		this.windowVisible = true;

	}

	public void fecharWindow(ActionEvent event) {
		posts = new ArrayList<PostEntity>();
		visiblePosts = new ArrayList<PostEntity>();
		clear();
		this.windowVisible = false;

	}

	public boolean isWindowVisible() {
		return windowVisible;
	}

	public void toBack(ActionEvent event) {
		pagina--;
		filtrar();
	}

	public void toFront(ActionEvent event) {
		pagina++;
		filtrar();
	}

	public boolean isAnterior() {
		if (posts.size() > 0) {
			return posts.get(0).getId().equals(visiblePosts.get(0).getId());
		} else {
			return true;
		}
	}

	public boolean isProximo() {
		if (posts.size() > 0) {
			return posts.get(posts.size() - 1).getId().equals(
					visiblePosts.get(visiblePosts.size() - 1).getId());
		} else {
			return true;
		}
	}

	private void filtrar() {
		visiblePosts.clear();
		for (int i = 0; i < NUM_POSTS; i++) {
			int index = ((pagina - 1) * NUM_POSTS) + i;
			if (index < posts.size()) {
				visiblePosts.add(posts.get(index));
			} else {
				return;
			}

		}
	}

	private void clear() {
		this.criterioTag = "";
		this.criterioEmail = "";
		this.dataFinal = null;
		this.dataInicial = null;
		this.posts.clear();
		this.visiblePosts.clear();
	}

	public List<PostEntity> getPosts() {
		return visiblePosts;
	}

	public Date getDataInicial() {
		return dataInicial;
	}

	public void setDataInicial(Date dataInicial) {
		this.dataInicial = dataInicial;
	}

	public Date getDataFinal() {
		return dataFinal;
	}

	public void setDataFinal(Date dataFinal) {
		this.dataFinal = dataFinal;
	}

	public String getCriterioTag() {
		return criterioTag;
	}

	public void setCriterioTag(String criterioTag) {
		this.criterioTag = criterioTag;
	}

	public List<PostEntity> getVisiblePosts() {
		return visiblePosts;
	}

	public String getCriterioEmail() {
		return criterioEmail;
	}

	public void setCriterioEmail(String criterioEmail) {
		this.criterioEmail = criterioEmail;
	}

	public String getSelectedGroup() {
		return selectedGroup;
	}

	public void setSelectedGroup(String selectedGroup) {
		this.selectedGroup = selectedGroup;
	}

	public void excluirPost(ActionEvent event) {
		Long postId = Long.parseLong((String) event.getComponent()
				.getAttributes().get("postId"));

		PostEntity removePost = null;

		for (PostEntity postEntity : posts) {
			if (postEntity.getPost().getId().longValue() == postId.longValue()) {
				removePost = postEntity;
				break;
			}
		}

		posts.remove(removePost);
		visiblePosts.remove(removePost);

		try {
			Post post = Post.buscarPorId(postId);
			Usuario usuario = post.getUsuario();
			usuario.excluirPost(post);
			EntityManagerService.salvar(usuario);

		} catch (Exception e) {
			e.printStackTrace();
		}

		filtrar();
	}

	public void excluirUsuario(ActionEvent e) {
		try {
			Usuario u = Usuario.buscarPorEmail(criterioEmail);
			u.clearPosts();
			u.setBloqueado(true);
			EntityManagerService.salvar(u);
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		clear();
	}

	public void comentar(ActionEvent event){
		String postId = (String) event.getComponent().getAttributes().get(
		"postId");
		ComentarioMBean bean = (ComentarioMBean) FacesContext.getCurrentInstance().getExternalContext().getRequestMap().get("comentarioMBean");
		bean.comentarioPost(postId);
	}

}
